package com.fastloan.jn.mgt.customermanager.train.service.impl;

import com.fastloan.jn.common.BasicServiceImpl;
import com.fastloan.jn.mgt.customermanager.train.dao.JnManagerTrainMapper;
import com.fastloan.jn.mgt.customermanager.train.model.JnManagerTrain;
import com.fastloan.jn.mgt.customermanager.train.model.vo.ManagerTrainExt;
import com.fastloan.jn.mgt.customermanager.train.service.JnManagerTrainService;
import com.kawins.supers.table.manager.DataTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by gelon on 2017/10/26.
 */
@Service
public class JnManagerTrainServiceImpl extends BasicServiceImpl<JnManagerTrain> implements JnManagerTrainService {


    @Autowired
    JnManagerTrainMapper trainMapper ;

    @Override
    @DataTable(name = "selectManagerTrain")
    public List<ManagerTrainExt> selectManagerTrain(Map<String, Object> map) {
        return trainMapper.selectManagerTrain(map);
    }

    @Override
    public void changeTrainManagers(String[] addIds, String[] removeIds, String trainId, String userId) {
        Arrays.stream(addIds).forEach(addid -> insertSelective(createManagerTrain(addid,trainId,userId)));
        Arrays.stream(removeIds).forEach(removeId -> deleteManagerTrain(removeId,trainId));
    }

    private void deleteManagerTrain(String removeId, String trainId) {
        JnManagerTrain jnManagerTrain = selectOne(new JnManagerTrain().setTrainId(trainId).setUserId(removeId));
        deleteByPrimaryKey(jnManagerTrain);
    }

    private JnManagerTrain createManagerTrain(String addid, String trainId, String createId) {
        JnManagerTrain managerTrain = new JnManagerTrain();
        managerTrain.setTrainId(trainId);
        managerTrain.setUserId(addid);
        managerTrain.setCreater(createId);
        managerTrain.setCreateTime(new Date());
        managerTrain.setStatus((short)1);
        return managerTrain;
    }
}
